//
//  ByImageNewsCell.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByImageNewsCell.h"


@implementation ByImageNewsCell

+ (instancetype)createCellInTableview:(UITableView *)table {
    
    static NSString *cellIdentifier = @"ByImageNewsCell";
    ByImageNewsCell *cell = [table dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setModels:(NSArray *)models {
    _models = models;
    [self setNeedsLayout];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    for (UIView *subview in self.contentView.subviews) {
        [subview removeFromSuperview];
    }
    
    CGFloat x = 0, y = 0, width = (SCREEN_WIDTH - 20 - 5) / 2, height = 0;
    for (int i = 0; i < self.models.count; i ++) {
        ByNetNewsItemModel *model = self.models[i];
        height = width + 30;
        x = (i % 2 == 0) ? 10 : (10 + width + 5);
        y = (i / 2 == 0) ? 10 : (10 + height * (i / 2));
        UIView *basalView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [self.contentView addSubview:basalView];
        basalView.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [basalView addGestureRecognizer:tap];
        
        /** 图片 */
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, basalView.width, basalView.width)];
        [imageView setImageWithURL:[NSURL URLWithString:model.main_img] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
        [basalView addSubview:imageView];
        
        /** 内容 */
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.bottom, imageView.width, 30)];
        contentLabel.textColor = [UIColor blackColor];
        contentLabel.font = FONT(14);
        contentLabel.textAlignment = NSTextAlignmentCenter;
        [basalView addSubview:contentLabel];
        contentLabel.text = model.post_title;
    }
}


- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSInteger tag = tap.view.tag;
    NSLog(@"%li", tag);
    ByNetNewsItemModel *model = self.models[tag];
    NSString *postID = model.id;
    NSString *urlStr = [NSString stringWithFormat:@"Webview/Post/viewPost?id=%@", postID];
    ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:urlStr];
    vc.navigationItem.title = model.post_cate_name;
    [self.viewController.navigationController pushViewController:vc animated:YES];

}
@end
